﻿using System;
using System.Collections.Generic;
using System.Linq;
using nanoCMS.Core.Concrete;
using nanoCMS.Core.WebProviders.ViewModels;

namespace nanoCMS.Core.WebProviders {
	public class ViewElementProvider {
		private readonly WorldContext _worldContext;


		public ViewElementProvider(WorldContext worldContext) {
			_worldContext = worldContext;
		}


		#region Factories
		private static ViewElementVM ViewElementVmFactoryFromTenantPageStructure(TenantPageStructure tenantPage) {
			ViewElementVM model = new ViewElementVM {
				Action = tenantPage.Method,
				ElementKey = tenantPage.ID.ToString()
			};


			model.RouteValues.Add("area", tenantPage.Area);
			model.RouteValues.Add("controller", tenantPage.Controller);
			if (tenantPage.Parameters != null)
				foreach (var keyValuePair in tenantPage.Parameters.Split('&')) {
					model.RouteValues.Add(keyValuePair.Split('=')[0], keyValuePair.Split('=')[1]);
				}

			return model;
		}

		private static ViewElementVM ViewElementVmFactoryFromStaticPageStructure(StaticPageStructure staticPageStructure)
		{
			ViewElementVM model = new ViewElementVM {
				Action = staticPageStructure.Action,
				ElementKey = staticPageStructure.ID.ToString()
			};

			model.RouteValues.Add("area", staticPageStructure.Area);
			model.RouteValues.Add("controller", staticPageStructure.Controller);
			if (staticPageStructure.Parameters != null)
				foreach (var keyValuePair in staticPageStructure.Parameters.Split('&')) {
					model.RouteValues.Add(keyValuePair.Split('=')[0], keyValuePair.Split('=')[1]);
				}

			return model;
		}
		#endregion


		/// <summary>
		/// Get section elements for normal page
		/// </summary>
		/// <param name="tenantPageID"></param>
		/// <param name="tenantPageTemplateID"></param>
		/// <param name="sectionType"></param>
		/// <returns></returns>
		public List<ViewElementVM> GetSection(int tenantPageID, int tenantPageTemplateID, int sectionType) {
			return
				 _worldContext.Entities.TenantPageStructures.Where(
					  x =>
					  (x.TenantPageID == tenantPageID ||
					  x.PageTemplateID == tenantPageTemplateID)
					  && x.SectionTypeID == sectionType).OrderBy(x => x.Order).Select(ViewElementVmFactoryFromTenantPageStructure).ToList();
		}

		/// <summary>
		/// Gets section elements for StaticPage
		/// </summary>
		/// <param name="tenantPageTemplateID"></param>
		/// <param name="pageSection"></param>
		/// <returns></returns>
		public List<ViewElementVM> GetStaticSection(int tenantPageTemplateID, PageSections pageSection)
		{
			int sectionType = Convert.ToInt32(pageSection);
			return
				 _worldContext.Entities.TenantPageStructures.Where(
					  x =>
					  (x.PageTemplateID == tenantPageTemplateID)
					  && x.SectionTypeID == sectionType).OrderBy(x => x.Order).Select(ViewElementVmFactoryFromTenantPageStructure).ToList();
		}

		public List<ViewElementVM> GetStaticSectionMain(string link)
		{
			return
				_worldContext.Entities.StaticPageStructures.Where(x => x.PageUrl == link).OrderBy(x => x.Order).Select(
					ViewElementVmFactoryFromStaticPageStructure).ToList();
		} 
	}
}
